'use strict';

define('sf.weixin', [
    'zepto',
    'can',
    'jweixin',
    'sf.util',
    'store',
    'sf.b2c.mall.api.user.getWeChatJsApiSig'
], function($, can, jweixin, SFUtil, store, SFGetWeChatJsApiSig) {

    var createNonceStr = function() {
        return Math.random().toString(36).substr(2, 15);
    };

    var createTimestamp = function() {
        return parseInt(new Date().getTime() / 1000) + '';
    };

    var gethHostUrl = function() {
        // return window.location.origin + window.location.pathname;
        return window.location.href;
    };

    var configWeixin = function() {
        var noncestr = createNonceStr();
        var timestamp = createTimestamp();

        var ret = {
            "keyValuePairs": JSON.stringify([{
                "value": noncestr,
                "key": "noncestr"
            }, {
                "value": timestamp,
                "key": "timestamp"
            }, {
                "value": gethHostUrl(),
                "key": "url"
            }])
        };

        var getWeChatJsApiSig = new SFGetWeChatJsApiSig(ret);

        getWeChatJsApiSig
            .sendRequest()
            .done(function(data) {
                jweixin.config({
                    "debug": false,
                    "appId": 'wxba06a7bc806357c3',
                    "timestamp": timestamp,
                    "nonceStr": noncestr,
                    "signature": data.value,
                    "jsApiList": [
                        'onMenuShareTimeline', 'onMenuShareAppMessage', 'imagePreview'
                    ]
                });
            })
            .fail(function(error) {
                console.error(error);
            })
    };

    return {
        /** [shareIndex 分享首页] */
        shareIndex: function() {

            if (SFUtil.isMobile.WeChat()) {
                //进行微信设置
                configWeixin();
            }

            // 定义微信分享的数据
            jweixin.ready(function() {

                jweixin.onMenuShareTimeline({
                    title: '顺丰海淘--别再淘宝啦！快来顺丰海淘，挑海外好货，一起提升B格！',
                    desc: '别再淘宝啦！快来顺丰海淘，挑海外好货，一起提升B格！',
                    link: 'http://m.tepin.hk/index.html',
                    imgUrl: 'http://img0.fengqucdn.com/sfht/img/sharelog.png',
                    trigger: function(res) {
                        // alert('用户点击发送给朋友');
                    },
                    success: function(res) {
                        // alert('已分享');
                    },
                    cancel: function(res) {
                        // alert('已取消');
                    },
                    fail: function(res) {
                        //alert(JSON.stringify(res));
                    }
                });

                jweixin.onMenuShareAppMessage({
                    title: '顺丰海淘--别再淘宝啦！快来顺丰海淘，挑海外好货，一起提升B格！',
                    desc: '别再淘宝啦！快来顺丰海淘，挑海外好货，一起提升B格！',
                    link: 'http://m.tepin.hk/index.html',
                    imgUrl: 'http://img0.fengqucdn.com/fqhth5/h5-common-oss/2.0.24/img/sharelog.png',
                    trigger: function(res) {
                        // alert('用户点击发送给朋友');
                    },
                    success: function(res) {
                        // alert('已分享');
                    },
                    cancel: function(res) {
                        // alert('已取消');
                    },
                    fail: function(res) {
                        //alert(JSON.stringify(res));
                    }
                });

                jweixin.showMenuItems({
                    menuList: ['menuItem:share:timeline', 'menuItem:share:appmessage', 'imagePreview']
                });
            })
        },
        share: function(params) {
            if (SFUtil.isMobile.WeChat()) {
                configWeixin();
            }

            params = params || {};

            var empty = function() {};

            // 定义微信分享的数据
            jweixin.ready(function() {
                jweixin.onMenuShareTimeline({
                    title: params.title,
                    desc: params.desc,
                    link: params.link,
                    imgUrl: params.imgUrl,
                    trigger: params.trigger || empty,
                    success: params.success || empty,
                    cancel: params.cancel || empty,
                    fail: params.fail || empty
                });

                jweixin.onMenuShareAppMessage({
                    title: params.title,
                    desc: params.desc,
                    link: params.link,
                    imgUrl: params.imgUrl,
                    trigger: params.trigger || empty,
                    success: params.success || empty,
                    cancel: params.cancel || empty,
                    fail: params.fail || empty
                });

                jweixin.showMenuItems({
                    menuList: ['menuItem:share:timeline', 'menuItem:share:appmessage']
                });
            });
        },

        /**
         * [shareDetail 分享详情页]
         * @param  {[type]} title  [标题]
         * @param  {[type]} desc   [描述]
         * @param  {[type]} link   [链接]
         * @param  {[type]} imgUrl [图片链接]
         */
        shareDetail: function(title, desc, link, imgUrl, successCallback) {
            var that = this;

            if (SFUtil.isMobile.WeChat()) {
                //进行微信设置
                configWeixin();
            }

            // 定义微信分享的数据
            jweixin.ready(function() {
                //进行微信设置

                jweixin.onMenuShareTimeline({
                    title: title,
                    desc: desc,
                    link: link,
                    imgUrl: imgUrl,
                    trigger: function(res) {
                        // alert('用户点击发送给朋友');
                    },
                    success: function(res) {
                        if (typeof successCallback != 'undefined') {
                            successCallback.call(this);
                        }
                        // alert('已分享');
                    },
                    cancel: function(res) {
                        // alert('已取消');
                    },
                    fail: function(res) {
                        //alert(JSON.stringify(res));
                    }
                });

                jweixin.onMenuShareAppMessage({
                    title: title,
                    desc: desc,
                    link: link,
                    imgUrl: imgUrl,
                    trigger: function(res) {
                        // alert('用户点击发送给朋友');
                    },
                    success: function(res) {
                        if (typeof successCallback != 'undefined') {
                            successCallback.call(this);
                        }
                        // alert('已分享');
                    },
                    cancel: function(res) {
                        // alert('已取消');
                    },
                    fail: function(res) {
                        //alert(JSON.stringify(res));
                    }
                });

                jweixin.showMenuItems({
                    menuList: ['menuItem:share:timeline', 'menuItem:share:appmessage']
                });
            })
        },

        /**
         * [shareLuckyMoney 分享红包]
         * @param  {[type]} title  [标题]
         * @param  {[type]} desc   [描述]
         * @param  {[type]} id   [红包ID]
         */
        shareInvitation: function(title, desc, bagid, userid) {

            var that = this;

            var shareUrl = "http://m.tepin.hk/center/invitation-bag.html?_ruser=" + userid + "&bagid=" + bagid;

            if (SFUtil.isMobile.WeChat()) {
                //进行微信设置
                configWeixin();
            }

            // 定义微信分享的数据
            jweixin.ready(function() {

                jweixin.onMenuShareTimeline({
                    title: title,
                    desc: desc,
                    link: shareUrl,
                    imgUrl: 'http://img0.fengqucdn.com/sfhth5/1.1.2/img/luckymoneyshare.jpg',
                    trigger: function(res) {
                        //alert('用户点击发送给朋友圈');
                    },
                    success: function(res) {
                        //alert('已分享');
                    },
                    cancel: function(res) {
                        //alert('已取消');
                    },
                    fail: function(res) {
                        //alert(JSON.stringify(res));
                    }
                });

                jweixin.onMenuShareAppMessage({
                    title: title,
                    desc: desc,
                    link: shareUrl,
                    imgUrl: 'http://img0.fengqucdn.com/sfhth5/1.1.2/img/luckymoneyshare.jpg',
                    trigger: function(res) {
                        //alert('用户点击发送给朋友');
                    },
                    success: function(res) {
                        //alert('已分享');
                    },
                    cancel: function(res) {
                        //alert('已取消');
                    },
                    fail: function(res) {
                        //alert(JSON.stringify(res));
                    }
                });

                jweixin.showMenuItems({
                    menuList: ['menuItem:share:timeline', 'menuItem:share:appmessage']
                });
            })
        },

        /**
         * [shareLuckyMoney 分享红包]
         * @param  {[type]} title  [标题]
         * @param  {[type]} desc   [描述]
         * @param  {[type]} id   [红包ID]
         */
        shareLuckyMoney: function(title, desc, id) {

            var that = this;
            /*
            var params = {
              appid: "wx90f1dcb866f3df60",
              redirect_uri: "http://m.tepin.hk/luckymoneyaccept.html",
              response_type: "code",
              scope: "snsapi_base",
              state: id
            };
            var shareUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?"
              + $.param(params) + "#wechat_redirect"
            */
            var shareUrl = "http://m.tepin.hk/other/luckymoneyaccept.html?id=" + id;

            if (SFUtil.isMobile.WeChat()) {
                //进行微信设置
                configWeixin();
            }

            // 定义微信分享的数据
            jweixin.ready(function() {

                jweixin.onMenuShareTimeline({
                    title: title,
                    desc: desc,
                    link: shareUrl,
                    imgUrl: 'http://img0.fengqucdn.com/sfhth5/1.1.2/img/luckymoneyshare.jpg',
                    trigger: function(res) {
                        //alert('用户点击发送给朋友圈');
                    },
                    success: function(res) {
                        //alert('已分享');
                    },
                    cancel: function(res) {
                        //alert('已取消');
                    },
                    fail: function(res) {
                        //alert(JSON.stringify(res));
                    }
                });

                jweixin.onMenuShareAppMessage({
                    title: title,
                    desc: desc,
                    link: shareUrl,
                    imgUrl: 'http://img0.fengqucdn.com/sfhth5/1.1.2/img/luckymoneyshare.jpg',
                    trigger: function(res) {
                        //alert('用户点击发送给朋友');
                    },
                    success: function(res) {
                        //alert('已分享');
                    },
                    cancel: function(res) {
                        //alert('已取消');
                    },
                    fail: function(res) {
                        //alert(JSON.stringify(res));
                    }
                });

                jweixin.showMenuItems({
                    menuList: ['menuItem:share:timeline', 'menuItem:share:appmessage']
                });
            })
        }
    };
});
